package com.cskaoyan.gateway.controller.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.order.vo.OrderDetailVO;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.user.constants.SysRetCodeConstants;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author tean_cy
 * @date 2021/1/23 16:15
 */

@Slf4j
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Reference(timeout = 99999,retries = 0,check = false)
    OrderCoreService orderCoreService;

    /**
     * 创建当前用户的订单
     *
     * @param createOrderRequest
     * @return
     */
    @PostMapping("/order")
    @ApiOperation(value = "创建用户订单", httpMethod = "post")
    // @ApiImplicitParam(name = "form", value = "地址信息", dataType = "AddressForm", required = true)
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest servletRequest) {
        if((createOrderRequest.getOrderTotal().compareTo(new BigDecimal(0)))==0){
            return new ResponseUtil().setErrorMsg("购买数量不能为0");
        }
        String userInfo = (String) servletRequest.getAttribute("userInfo");
        JSONObject object = JSON.parseObject(userInfo);
        long uid = Long.parseLong(object.get("uid").toString());
        createOrderRequest.setUserId(uid);
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());

        CreateOrderResponse response = orderCoreService.createOrder(createOrderRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getOrderId());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @DeleteMapping("/order/{id}")
    @ApiOperation(value = "删除用户订单", httpMethod = "delete")
    public ResponseData deleteOrder(@PathVariable String  id) {
        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(id);

        DeleteOrderResponse response = orderCoreService.deleteOrder(deleteOrderRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @RequestMapping("/cancelOrder")
    @ApiOperation(value = "取消用户订单", httpMethod = "post")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {
        CancelOrderResponse response = orderCoreService.cancelOrder(cancelOrderRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }


    @GetMapping("/order")
    @ApiOperation("获取当前用户的所有订单")
    public ResponseData getOrders(@RequestParam("size") String size,
                                  @RequestParam("page") String page,
                                  HttpServletRequest request) {
        String userInfo = (String) request.getAttribute("userInfo");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long uid = Long.parseLong(jsonObject.get("uid").toString());

        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setUserId(uid);
        try {
            orderListRequest.setPage(Integer.parseInt(page));
            orderListRequest.setSize(Integer.parseInt(size));
        } catch (Exception e){
            return new ResponseUtil().setErrorMsg(OrderRetCode.SYSTEM_ERROR.getMessage());
        }

        OrderListResponse response = orderCoreService.getOrders(orderListRequest);
        if (response==null){
            return new ResponseUtil().setErrorMsg("错误的用户参数");
        }
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            Map map = new HashMap();
            map.put("data", response.getDetailInfoList());
            map.put("total", response.getTotal());
            return new ResponseUtil().setData(map, response.getMsg());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("/order/{id}")
    @ApiOperation("查询订单详情")
    public ResponseData getOrderDetail(@PathVariable("id")Long orderId, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute("userInfo");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());
        OrderDetailVO response = orderCoreService.getOrderDetail(orderId, uid);
        //
        if (response == null || response.getUserId() == null || response.getUserName() == null) {
            return new ResponseUtil().setErrorMsg("错误的订单参数");
        }
        return new ResponseUtil().setData(response);
    }
}
